package server.modules.generator.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import server.common.utils.PageUtils;
import server.common.utils.Query;

import server.modules.generator.dao.MeetNoticeDao;
import server.modules.generator.entity.MeetNoticeEntity;
import server.modules.generator.entity.MeetingEntity;
import server.modules.generator.entity.ProjectEntity;
import server.modules.generator.service.MeetNoticeService;
import server.modules.generator.service.MeetingService;
import server.modules.sys.entity.SysUserEntity;
import server.modules.sys.service.SysUserService;


@Service("meetNoticeService")
public class MeetNoticeServiceImpl extends ServiceImpl<MeetNoticeDao, MeetNoticeEntity> implements MeetNoticeService {
    @Autowired
    MeetingService meetingService;
    @Autowired
    SysUserService sysUserService;

    public  Date getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        return today;
    }
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Long toUser = (Long) params.get("toUser");
        String flag = (String) params.get("flag");
        String key = (String) params.get("key");
        IPage<MeetNoticeEntity> page = this.page(
                new Query<MeetNoticeEntity>().getPage(params),
                new QueryWrapper<MeetNoticeEntity>()
                .eq(null!=toUser,"to_user_id",toUser)
                .eq(null!=flag,"flag",flag)
                .ge(null!=key&&key.equals("week"),"create_time",getPastDate(7))
                .ge(null!=key&&key.equals("month"),"flag",getPastDate(30))
                .orderByDesc("create_time")
        );


        for (MeetNoticeEntity sing : page.getRecords()) {
            MeetingEntity meetingEntity = meetingService.getById(sing.getMeetId());
            if (null != meetingEntity)
                sing.setMeetName(meetingEntity.getTitle());
            else
                sing.setMeetName("未配置");



            SysUserEntity sysUserEntity = sysUserService.getById(sing.getToUserId());
            if (null != sysUserEntity)
                sing.setToUserName(sysUserEntity.getShowname());
            else
                sing.setMeetName("未配置");
        }


        return new PageUtils(page);
    }

}